<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import type { FormInstance, FormRules, UploadInstance } from 'element-plus'
import { usePublicStoreHook } from '@/store/modules/public';
import { useUserStore } from '@/store/modules/user';
import { fetchPostSociety } from '@/api/public/societyApply'
import { UploadFilled } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import Upload from '@/components/upload/index.vue'
import { fetchGetSocietyInfo } from '@/api/society/index'


const uploadRef = ref<any>(null)
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  name: '',// 社团名称
  type: 0, // 社团类型
  introduce: '',//社团简介
  planNum: 0,//招聘人数
  reason: '',// 申请原因
  publicizeUrl: '', // 图片
  attachment: '', // 附件
  personId: useUserStore().userInfo.id,
  applyType: 2
})

let societyTypeList = reactive([
  {
    id: '',
    name: ''
  }
])
const rules = reactive<FormRules>({
  type: [
    { required: true, message: '请选择', trigger: 'change' },
  ],
  name: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  introduce: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  planNum: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  publicizeUrl: [
    { required: true, message: '请选择', trigger: 'blur' },
  ],
  reason: [
    { required: true, message: '请填写', trigger: 'blur' },
  ],
})

// 提交表单
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      uploadRef.value.submitUpload()
      setTimeout(() => {
        fetchPostSociety(ruleForm).then((res) => {
          if (Number(res.code) === 200) {
            ElMessage({
              type: 'success',
              message: '申请提交成功，请等待审批',
            })
          } else {
          }
        })
      }, 500)
    } else {
      ElMessage({
        type: 'warning',
        message: '请正确填写',
      })
    }
  });
};

// 重置表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
onMounted(() => {
  societyTypeList = usePublicStoreHook().societyTypeList
  getSocietyInfo()
})
const fileUrl = (value: any) => {
  ruleForm.publicizeUrl = value
}

let societyInfo = ref({
  planNumber: 0,
})
// 获取社团信息
const getSocietyInfo = () => {
  const id = useUserStore().userInfo.society.split('-')[1]
  fetchGetSocietyInfo(id).then((res: { data: { name: string, type: number, introduce: string, planNumber: number; imgurl: string }; }) => {
    societyInfo.value = res.data
    const { name, type, introduce, planNumber, imgurl } = res.data
    ruleForm.name = name
    ruleForm.type = type
    ruleForm.introduce = introduce
    ruleForm.planNum = planNumber
    ruleForm.publicizeUrl = imgurl
  })
  console.log()
}

</script>
<template>
  <div class="create-society-container">
    <div class="container">
      <div class="container-header-name">修改社团信息</div>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" label-position="top"
        class="demo-ruleForm" size="default" status-icon>
        <el-form-item label="社团名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="社团类型" prop="type" class="el-form-item-full">
          <el-select v-model="ruleForm.type" class="m-2" placeholder="请选择社团类型" :key="societyTypeList.length">
            <el-option v-for="item in societyTypeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="社团简介" prop="introduce">
          <el-input v-model="ruleForm.introduce" type="textarea" :rows="5" />
        </el-form-item>
        <el-form-item label="招聘人数" prop="planNum">
          <el-input v-model="ruleForm.planNum" />
        </el-form-item>
        <el-form-item label="社团宣传图" class="el-form-item-full">
          <Upload type="img" ref="uploadRef" @fileUrl="fileUrl" />
        </el-form-item>
        <el-form-item label="社团宣传图" class="el-form-item-full">
          <img :src="ruleForm.publicizeUrl" style="height: 200px;">
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm(ruleFormRef)" size="large" class="btn-white">重置</el-button>
          <el-button type="primary" size="large" @click="submitForm(ruleFormRef)" class="btn-green">
            提交
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.create-society-container {
  display: flex;
  justify-content: center;
  .container {
    width: 50%;

    .container-header-name {
      background: $green;
      height: 6rem;
      line-height: 6rem;
      font-size: 2rem;
      font-weight: 700;
      color: #fff;
      padding: 0 2rem;
    }

    .header-disabled {
      background: #999;
    }

    .demo-ruleForm {
      border: 0.5rem solid $green;
      border-top: none;
      padding: 2rem;

      .el-form-item__content {
        display: grid !important;
      }
    }

    .form-disabled {
      border-color: #999;
    }

    .el-form-item {
      margin-bottom: 2rem;
    }
  }
}
</style>

